//
//  Z_FindingNewViewController.m
//  SpaceMovie
//
//  Created by nana on 15/3/14.
//  Copyright (c) 2015年 nana. All rights reserved.
//

#import "Z_FindingNewViewController.h"

#import "Connection.h"//网络请求
#import "UIImageView+WebCache.h"//图片下载

#import "Z_FindingTableViewCell.h"//发现>新闻Cell
#import "Z_FindingPhotoTableViewCell.h"//发现>新闻图片Cell
#import "Z_FindingNewImageModel.h"//发现>新闻图片Model
//#import "Z_FindingNewDetaileViewController.h"//发现>新闻详情

#import "Z_FindingTrailerTableViewCell.h"//发现>预告片


@interface Z_FindingNewViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation Z_FindingNewViewController
- (void)dealloc
{
    [_headerArray release];
    [_headerModel release];
    [_scroll release];
    [_array release];
    [_tableView release];
    [_findingNewModel release];
    [_arrTrailer release];
    [_trailer release];
    [_trailerModel release];
    
    
    
    
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];//发现>新闻数组
        self.findingNewModel = [[Z_FindingNewModel alloc] init];//发现>新闻model
        [_findingNewModel release];
        
        
        self.arrTrailer = [NSMutableArray array];//发现>预告片数组
        self.trailerModel = [[Z_FindingTrailerModel alloc] init];//发现>预告片model
        [_trailerModel release];
        
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"新闻"];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    [self beginHeaderConnection];
    [self beginConnection];
    
}
- (void)creatSubview
{
    //创建表头菜单
    UIView *aView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, 64))];
    [aView setBackgroundColor:[UIColor grayColor]];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"新闻", @"预告片", @"排行榜", @"影评"]];
    seg.frame = CGRectMake(0, 18, self.view.frame.size.width, 45);
    [aView addSubview:seg];
    [self.view addSubview:seg];
    
    //创建滚屏
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 63, self.view.frame.size.width, self.view.frame.size.height - 50)];
    [_scroll setBackgroundColor:[UIColor redColor]];
    _scroll.contentSize = CGSizeMake(self.scroll.frame.size.width * 4, self.scroll.frame.size.height);
    _scroll.pagingEnabled = YES;
    
    //新闻tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _scroll.frame.size.width, _scroll.frame.size.height) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tag = 0;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width - 70))];//表头图片
    [imgView setImageWithURL:[NSURL URLWithString:_headerModel.findingNewHeader_img]];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, imgView.frame.size.height - 60, self.view.frame.size.width, 60))];//表头图片上面的文字
    titleLabel.alpha = 0.8;
    [titleLabel setBackgroundColor:[UIColor blackColor]];
    titleLabel.text = _headerModel.findingNewHeader_title;
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont systemFontOfSize:22];
    titleLabel.textColor = [UIColor whiteColor];
    [imgView addSubview:titleLabel];
    _tableView.tableHeaderView = imgView;
    [_scroll addSubview:_tableView];
    [_tableView release];
    
    //预告片tableView
    self.trailer = [[UITableView alloc] initWithFrame:(CGRectMake(_scroll.frame.size.width, 0, _scroll.frame.size.width, _scroll.frame.size.height)) style:(UITableViewStylePlain)];
    _trailer.tag = 1;
    _trailer.delegate = self;
    _trailer.dataSource = self;
    
    UIImageView *imgViewTrailer = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width - 70))];//表头图片
    [imgViewTrailer setBackgroundColor:[UIColor yellowColor]];
    UILabel *title = [[UILabel alloc] initWithFrame:(CGRectMake(0, imgViewTrailer.frame.size.height - 60, self.view.frame.size.width, 60))];
    title.alpha = 0.8;
    [title setBackgroundColor:[UIColor blackColor]];
    [imgViewTrailer addSubview:title];
    _trailer.tableHeaderView = imgViewTrailer;
    [_scroll addSubview:_trailer];
    [_trailer release];

    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    [self.view addSubview:_scroll];

    

}
//返回cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0) {
        return [_array count];
    }else {
        return [_arrTrailer count];
    }
}

//选择cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //新闻cell
    if (tableView.tag == 0) {
        _findingNewModel = [_array objectAtIndex:indexPath.row];
        //只有一张图片的cell
        if (_findingNewModel.findingNewImg_arr == nil) {
            Z_FindingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"11"];
            if (!cell) {
                cell = [[Z_FindingTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"11"];
            }
            Z_FindingNewModel *model = [[Z_FindingNewModel alloc] init];
            model = [_array objectAtIndex: indexPath.row];
            [cell setAllValues:model];
            return cell;
        }
        //有三张图片的cell
        else {
            Z_FindingPhotoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"22"];
            if (!photoCell) {
                photoCell = [[Z_FindingPhotoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"22"];
            }
            Z_FindingNewModel *model2 = [[Z_FindingNewModel alloc] init];
            model2 = [_array objectAtIndex: indexPath.row];
            [photoCell setAllValue2:model2];
            return photoCell;
        }
    }
    
    
    //预告片的cell
    else{
        Z_FindingTrailerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"33"];
        if (!cell) {
            cell = [[Z_FindingTrailerTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"33"];
        }
        Z_FindingTrailerModel *model = [[Z_FindingTrailerModel alloc] init];
        model = [_array objectAtIndex: indexPath.row];
        [cell setAllValues:model];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        _findingNewModel = [_array objectAtIndex:indexPath.row];
        if (_findingNewModel.findingNewImg_arr == nil){
            return 160;
        
        }else {        
            return 190;
        
        }
    }else{
        return 160;
    }
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (tableView.tag == 0) {
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    _findingNewModel = [_array objectAtIndex:indexPath.row];
//    if (_findingNewModel.findingNewImg_arr == nil) {
//        
//        Z_FindingNewDetaileViewController *detaile = [[Z_FindingNewDetaileViewController alloc] init];
//        detaile.findingNewModel = _findingNewModel;
//        [self.navigationController pushViewController:detaile animated:YES];
//    
//    }else {
//        
//        
//        
//    }
//    }
//}

#pragma mark
#pragma mark 网络请求
- (void)beginConnection
{
    // 新闻的网络请求
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"pageIndex"];
    __block UITableView *tabview = _tableView;
    [Connection connectionWithUrl:Z_FindingNew parmaters:dic block:^(NSData *data) {

        // 解析JSON
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dictionary objectForKey:@"newsList"];
        
        for (NSDictionary *dic in arr) {
            
            Z_FindingNewModel *newModel = [[Z_FindingNewModel alloc] init];
//            [newModel setValuesForKeysWithDictionary:dic];
            newModel.findingNew_id = [dic objectForKey:@"id"];
            newModel.findingNew_type = [dic objectForKey:@"type"];
            newModel.findingNew_imagePath = [dic objectForKey:@"image"];
            newModel.findingNew_title = [dic objectForKey:@"title"];
            newModel.findingNew_title2 = [dic objectForKey:@"title2"];
            newModel.findingNew_publishTime = [dic objectForKey:@"publishTime"];
            newModel.findingNew_commentCount = [dic objectForKey:@"commentCount"];
            newModel.findingNewImg_arr = [dic objectForKey:@"images"];
            newModel.imgModel.findingNewImage_dic1 = [newModel.findingNewImg_arr objectAtIndex:0];
            newModel.imgModel.findingNewImage_dic2 = [newModel.findingNewImg_arr objectAtIndex:1];
            newModel.imgModel.findingNewImage_dic3 = [newModel.findingNewImg_arr objectAtIndex:2];
            newModel.imgModel.findingNewImage_url1 = [newModel.imgModel.findingNewImage_dic1 objectForKey:@"url1"];
            newModel.imgModel.findingNewImage_url2 = [newModel.imgModel.findingNewImage_dic2 objectForKey:@"url1"];
            newModel.imgModel.findingNewImage_url3 = [newModel.imgModel.findingNewImage_dic3 objectForKey:@"url1"];
            [_array addObject:newModel];
        }
        [self creatSubview];
        //数据源一旦改变,刷新
        [tabview reloadData];
    }];
    
    // 预告片的网络请求

//    __block UITableView *tabviewTrailer = _trailer;
//    [Connection connectionWithUrl:Z_FindingTralier parmaters:nil block:^(NSData *data) {
//        
//        // 解析JSON
//        NSDictionary *dictionaryTrailer = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *arrTrailer = [dictionaryTrailer objectForKey:@"trailers"];
//        
//        for (NSDictionary *dic in arr) {
//
//    
    
//    }];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}

#pragma mark
#pragma mark 表头图片
- (void)beginHeaderConnection
{
    __block UITableView *tabview = _tableView;
    [Connection connectionWithUrl:Z_FindingNewHeader parmaters:nil block:^(NSData *data) {
        // 解析JSON
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dictionary1 = [dictionary objectForKey:@"news"];
        
        self.headerModel = [[Z_FindingNewHeaderModel alloc] init];
        _headerModel.findingNewHeader_title = [dictionary1 objectForKey:@"title"];
        _headerModel.findingNewHeader_img = [dictionary1 objectForKey:@"imageUrl"];
        // 数据源一旦改变,刷新
        [tabview reloadData];
        [self creatSubview];

    }];

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
